package cn.xinfei.xdecision.data.core.migrate;

import cn.xinfei.xdecision.common.migrate.ElementFunctionParamMapper;
import cn.xinfei.xdecision.common.migrate.ElementMapper;
import cn.xinfei.xdecision.common.migrate.ElementScriptMapper;
import cn.xinfei.xdecision.common.model.datax.datainterface.v2.VariableInterface;
import cn.xinfei.xdecision.common.model.datax.datamanage.v2.Variable;
import cn.xinfei.xdecision.common.model.datax.datamanage.v2.VariableParam;
import cn.xinfei.xdecision.common.model.datax.enums.*;
import cn.xinfei.xdecision.common.model.datax.migrate.Element;
import cn.xinfei.xdecision.common.model.datax.migrate.ElementFunctionParam;
import cn.xinfei.xdecision.common.service.datax.datainterface.v2.VariableInterfaceService;
import cn.xinfei.xdecision.common.service.datax.datamanage.v2.VariableParamService;
import cn.xinfei.xdecision.common.service.datax.datamanage.v2.VariableScriptService;
import cn.xinfei.xdecision.common.service.datax.datamanage.v2.VariableService;
import cn.xinfei.xdecision.common.utils.util.OKHttpClientManage;
import cn.xinfei.xdecision.data.core.util.ExcelUtils;
import cn.xinfei.xdecision.data.core.util.JsonUtils;
import cn.xinfei.xdecision.data.core.vo.CellDataVO;
import cn.xinfei.xdecision.data.core.vo.VariableMigrateDTO;
import cn.xinfei.xdecision.data.core.vo.VariableMigrateVO;
import cn.xinfei.xdecision.data.core.vo.migrate.MigrateVarReqVO;
import cn.xinfei.xdecision.data.thread.AsyncExecuteThreadPool;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MigrateVarService {

    @Resource
    ElementFunctionParamMapper elementFunctionParamMapper;

    @Resource
    ElementMapper elementMapper;

    @Resource
    ElementScriptMapper elementScriptMapper;

    @Resource
    VariableService variableService;

    @Resource
    VariableParamService variableParamService;

    @Resource
    VariableScriptService variableScriptService;

    @Resource
    VariableInterfaceService varInterfaceRelService;

    @Value("${xdecision.data.migrate.python_url}")
    private String pythonThirdPartyVarsReqUrl;

    @Value("${xdecision.data.migrate.biz_python_url}")
    private String pythonBizVarsReqUrl;

    private OKHttpClientManage okHttpClientManage = null;


    private static AsyncExecuteThreadPool migrateExecutor = null;

    @PostConstruct
    public void init() {
        migrateExecutor = new AsyncExecuteThreadPool(
                30,
                200,
                30,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1000),
                "migrate-var-executor-%d",
                new ThreadPoolExecutor.CallerRunsPolicy(),
                true,
                ExecutorSceneEnum.MIGRATE
        );

    }

    public List<List<String>> getPythonThirdPartyVars() {
        okHttpClientManage = OKHttpClientManage.getOkHttpClientManage(pythonThirdPartyVarsReqUrl, 30000, 30000);
        List<List<String>> pythonVarConfigInfoList = new ArrayList<>();
        try {
            String response = okHttpClientManage.get("");
            JSONObject responseJson = JSONObject.parseObject(response, JSONObject.class);
            pythonVarConfigInfoList = (List<List<String>>) responseJson.get("value");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("error in getPythonThirdPartyVars", e);
        }
        return pythonVarConfigInfoList;
    }

    public List<List<String>> getPythonThirdPartyVarsFromJsonFile() {
        List<List<String>> pythonVarConfigInfoList = new ArrayList<>();
        try {
            InputStream jsonStream = this.getClass().getClassLoader().getResourceAsStream("migrate/third_party_factor_dict.json");
            JSONObject fileJson = JsonUtils.readJsonFile(jsonStream, "third_party_factor_dict");
            pythonVarConfigInfoList = (List<List<String>>) fileJson.get("value");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("error in getPythonThirdPartyVars", e);
        }
        return pythonVarConfigInfoList;
    }


    public List<List<String>> getPythonThirdPartyCacheVars() {
        List<List<String>> pythonThirdPartyVars = getPythonThirdPartyVars();
        List<Element> allThridPartyCacheElements = elementMapper.getAllThirdpartyCacheElements();
        List<String> filterEles = allThridPartyCacheElements.stream()
                .map(Element::getId)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        return pythonThirdPartyVars.stream()
                .peek(var -> {
                    var.set(0, "third_cache_" + var.get(0));
                })
                .filter(var -> filterEles.contains(var.get(0)))
                .collect(Collectors.toList());
    }

    public List<List<String>> getPythonThirdPartyCacheVarsFromJsonFile() {
        List<List<String>> pythonThirdPartyVars = getPythonThirdPartyVarsFromJsonFile();
        List<Element> allThridPartyCacheElements = elementMapper.getAllThirdpartyCacheElements();
        List<String> filterEles = allThridPartyCacheElements.stream()
                .map(Element::getId)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        return pythonThirdPartyVars.stream()
                .peek(var -> {
                    var.set(0, "third_cache_" + var.get(0));
                })
                .filter(var -> filterEles.contains(var.get(0)))
                .collect(Collectors.toList());
    }


    public List<List<String>> getPythonBizVars() {
        okHttpClientManage = OKHttpClientManage.getOkHttpClientManage(pythonBizVarsReqUrl, 30000, 30000);
        List<List<String>> pythonVarConfigInfoList = new ArrayList<>();
        try {
            String response = okHttpClientManage.get("");
            JSONObject responseJson = JSONObject.parseObject(response, JSONObject.class);
            pythonVarConfigInfoList = (List<List<String>>) responseJson.get("value");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("error in getPythonThirdPartyVars", e);
        }
        return pythonVarConfigInfoList;
    }

    public List<List<String>> getPythonBizVarsFromJsonFile() {
        List<List<String>> pythonVarConfigInfoList = new ArrayList<>();
        try {
            InputStream jsonStream = this.getClass().getClassLoader().getResourceAsStream("migrate/internal_factor_dict.json");
            JSONObject fileJson = JsonUtils.readJsonFile(jsonStream, "internal_factor_dict");
            pythonVarConfigInfoList = (List<List<String>>) fileJson.get("value");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("error in getPythonBizVarsFromJsonFile", e);
        }
        return pythonVarConfigInfoList;
    }


    //    @Transactional(rollbackFor = Exception.class)
    public void migratePythonBasicVars(List<Element> pythonInputVars,
                                       List<Element> pythonOutputVars,
                                       List<Element> pythonInnerVars,
                                       MigrateVarReqVO migrateVarReqVO) {
        log.info("start migratePythonBasicVars...");
        long startTime = System.currentTimeMillis();
        List<Variable> inputVarList;
        List<Variable> outputVarList;
        List<Variable> innerVarList;

        inputVarList = Optional.ofNullable(pythonInputVars).orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    Variable variable = new Variable();
                    variable.setCreator("李尚勤");
                    variable.setModifier("李尚勤");
                    variable.setVarName(var.getName());
                    variable.setVarCode(var.getId());
                    variable.setVarDefaultValue(var.getDefaultValue());
                    variable.setIsUseType(0);
                    variable.setIsOutput(0);
                    variable.setIsInner(0);
                    variable.setIsInput(1);
                    variable.setIsCommon(0);
                    variable.setDataSourceId(0L);
                    variable.setVarType(1);
                    variable.setStatus(1);
                    variable.setIsDeleted("N");
                    variable.setOrganId(1L);
                    variable.setFolderId(migrateVarReqVO.getPythonBasicFolderId());
                    variable.setVarValueType(Integer.valueOf(
                            EnumUtil.fromNameOrThrow(VariableTypeEnum.class, var.getType()).getCode()));
                    variable.setInterfaceId(0L);
                    return variable;
                }).collect(Collectors.toList());

        outputVarList = Optional.ofNullable(pythonOutputVars).orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    Variable variable = new Variable();
                    variable.setCreator("李尚勤");
                    variable.setModifier("李尚勤");
                    variable.setVarName(var.getName());
                    variable.setVarCode(var.getId());
                    variable.setVarDefaultValue(var.getDefaultValue());
                    variable.setIsUseType(0);
                    variable.setIsOutput(1);
                    variable.setIsInner(0);
                    variable.setIsInput(0);
                    variable.setIsCommon(0);
                    variable.setDataSourceId(0L);
                    variable.setVarType(1);
                    variable.setStatus(1);
                    variable.setIsDeleted("N");
                    variable.setOrganId(1L);
                    variable.setFolderId(migrateVarReqVO.getPythonBasicFolderId());
                    variable.setVarValueType(Integer.valueOf(
                            EnumUtil.fromNameOrThrow(VariableTypeEnum.class, var.getType()).getCode()));
                    variable.setInterfaceId(0L);
                    return variable;
                }).collect(Collectors.toList());


        innerVarList = Optional.ofNullable(pythonInnerVars).orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    Variable variable = new Variable();
                    variable.setCreator("李尚勤");
                    variable.setModifier("李尚勤");
                    variable.setVarName(var.getName());
                    variable.setVarCode(var.getId());
                    variable.setVarDefaultValue(var.getDefaultValue());
                    variable.setIsUseType(0);
                    variable.setIsOutput(0);
                    variable.setIsInner(1);
                    variable.setIsInput(0);
                    variable.setIsCommon(0);
                    variable.setDataSourceId(0L);
                    variable.setVarType(1);
                    variable.setStatus(1);
                    variable.setIsDeleted("N");
                    variable.setOrganId(1L);
                    variable.setFolderId(migrateVarReqVO.getPythonBasicFolderId());
                    variable.setVarValueType(Integer.valueOf(
                            EnumUtil.fromNameOrThrow(VariableTypeEnum.class, var.getType()).getCode()));
                    variable.setInterfaceId(0L);
                    return variable;
                }).collect(Collectors.toList());

        List<Future<?>> futures = new ArrayList<>();
        List<List<Variable>> inputVarsPartitions = Lists.partition(inputVarList, 10);
        List<List<Variable>> outputVarsPartitions = Lists.partition(outputVarList, 10);
        List<List<Variable>> innerVarsPartitions = Lists.partition(innerVarList, 10);


        try {
            inputVarsPartitions.forEach(inputVar -> {
                futures.add(migrateExecutor.submit(() -> {
                    variableService.batchInsertOrUpdateVariables(inputVar);
                }));
            });

            outputVarsPartitions.forEach(outputVar -> {
                futures.add(migrateExecutor.submit(() -> {
                    variableService.batchInsertOrUpdateVariables(outputVar);
                }));
            });

            innerVarsPartitions.forEach(innerVar -> {
                futures.add(migrateExecutor.submit(() -> {
                    variableService.batchInsertOrUpdateVariables(innerVar);
                }));
            });

        } catch (Exception e) {
            log.error("error in migratePythonBasicVars", e);
        }
        for (Future<?> future : futures) {
            try {
                future.get();
            } catch (Exception e) {
                log.error("error in migratePythonBasicVars", e);
            }
        }

        long endTime = System.currentTimeMillis();
        log.info("stop migratePythonBasicVars...timeConsuming ={}", endTime - startTime);
    }

    //    @Transactional(rollbackFor = Exception.class)
    public void migrateJavaBasicVars(List<Element> javaInputVars,
                                     List<Element> javaOutputVars,
                                     List<Element> javaInnerVars,
                                     MigrateVarReqVO migrateVarReqVO) {
        log.info("start migrateJavaBasicVars...");
        long startTime = System.currentTimeMillis();

        List<Variable> inputVarList;
        List<Variable> outputVarList;
        List<Variable> innerVarList;

        inputVarList = Optional.ofNullable(javaInputVars).orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    Variable variable = new Variable();
                    variable.setCreator("李尚勤");
                    variable.setModifier("李尚勤");
                    variable.setVarName(var.getName());
                    variable.setVarCode(var.getId());
                    variable.setVarDefaultValue(var.getDefaultValue());
                    variable.setIsUseType(0);
                    variable.setIsOutput(0);
                    variable.setIsInner(0);
                    variable.setIsInput(1);
                    variable.setIsCommon(0);
                    variable.setDataSourceId(0L);
                    variable.setVarType(1);
                    variable.setStatus(1);
                    variable.setIsDeleted("N");
                    variable.setOrganId(1L);
                    variable.setFolderId(migrateVarReqVO.getJavaBasicFolderId());
                    variable.setVarValueType(Integer.valueOf(
                            EnumUtil.fromNameOrThrow(VariableTypeEnum.class, var.getType()).getCode()));
                    variable.setInterfaceId(0L);
                    return variable;
                }).collect(Collectors.toList());

        outputVarList = Optional.ofNullable(javaOutputVars).orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    Variable variable = new Variable();
                    variable.setCreator("李尚勤");
                    variable.setModifier("李尚勤");
                    variable.setVarName(var.getName());
                    variable.setVarCode(var.getId());
                    variable.setVarDefaultValue(var.getDefaultValue());
                    variable.setIsUseType(0);
                    variable.setIsOutput(1);
                    variable.setIsInner(0);
                    variable.setIsInput(0);
                    variable.setIsCommon(0);
                    variable.setDataSourceId(0L);
                    variable.setVarType(1);
                    variable.setStatus(1);
                    variable.setIsDeleted("N");
                    variable.setOrganId(1L);
                    variable.setFolderId(migrateVarReqVO.getJavaBasicFolderId());
                    variable.setVarValueType(Integer.valueOf(
                            EnumUtil.fromNameOrThrow(VariableTypeEnum.class, var.getType()).getCode()));
                    variable.setInterfaceId(0L);
                    return variable;
                }).collect(Collectors.toList());

        innerVarList = Optional.ofNullable(javaInnerVars).orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    Variable variable = new Variable();
                    variable.setCreator("李尚勤");
                    variable.setModifier("李尚勤");
                    variable.setVarName(var.getName());
                    variable.setVarCode(var.getId());
                    variable.setVarDefaultValue(var.getDefaultValue());
                    variable.setIsUseType(0);
                    variable.setIsOutput(0);
                    variable.setIsInner(1);
                    variable.setIsInput(0);
                    variable.setIsCommon(0);
                    variable.setDataSourceId(0L);
                    variable.setVarType(1);
                    variable.setStatus(1);
                    variable.setIsDeleted("N");
                    variable.setOrganId(1L);
                    variable.setFolderId(migrateVarReqVO.getJavaBasicFolderId());
                    variable.setVarValueType(Integer.valueOf(
                            EnumUtil.fromNameOrThrow(VariableTypeEnum.class, var.getType()).getCode()));
                    variable.setInterfaceId(0L);
                    return variable;
                }).collect(Collectors.toList());


        List<Future<?>> futures = new ArrayList<>();
        List<List<Variable>> inputVarsPartitions = Lists.partition(inputVarList, 20);
        List<List<Variable>> outputVarsPartitions = Lists.partition(outputVarList, 20);
        List<List<Variable>> innerVarsPartitions = Lists.partition(innerVarList, 20);

        try {
            inputVarsPartitions.forEach(inputVar -> {
                futures.add(migrateExecutor.submit(() -> {
                    variableService.batchInsertOrUpdateVariables(inputVar);
                }));
            });

            outputVarsPartitions.forEach(outputVar -> {
                futures.add(migrateExecutor.submit(() -> {
                    variableService.batchInsertOrUpdateVariables(outputVar);
                }));
            });

            innerVarsPartitions.forEach(innerVar -> {
                futures.add(migrateExecutor.submit(() -> {
                    variableService.batchInsertOrUpdateVariables(innerVar);
                }));
            });

        } catch (Exception e) {
            log.error("error in migrateJavaBasicVars", e);
        }

        for (Future<?> future : futures) {
            try {
                future.get();
            } catch (Exception e) {
                log.error("error in migrateJavaBasicVars", e);
            }
        }


        long endTime = System.currentTimeMillis();
        log.info("stop migrateJavaBasicVars...timeConsuming ={}", endTime - startTime);
    }

    public void migrateVarParams() {
        log.info("start migrateVarParams..");
        long startTime = System.currentTimeMillis();
        List<ElementFunctionParam> allFunctionParam = elementFunctionParamMapper.getAllFunctionParam();
        List<VariableParam> variableParams = allFunctionParam.stream()
                .map(param -> {
                    VariableParam variableParam = new VariableParam();
                    variableParam.setVarCode(param.getElementId());
                    variableParam.setParamVariableCode(param.getParamElementId());
                    variableParam.setOrganId(1L);
                    return variableParam;
                }).collect(Collectors.toList());

        List<Future<?>> futures = new ArrayList<>();
        List<List<VariableParam>> varParamsPartitions = Lists.partition(variableParams, 100);

        varParamsPartitions.forEach(varParam -> {
            futures.add(migrateExecutor.submit(() -> {
                variableParamService.saveBatch(varParam);
            }));
        });

        for (Future<?> future : futures) {
            try {
                future.get();
            } catch (Exception e) {
                log.error("error in migrateJavaThirdPartyVars", e);
            }
        }
        long endTime = System.currentTimeMillis();
        log.info("stop migrateJavaThirdPartyVars...timeConsuming ={}", endTime - startTime);
    }

    public List<String> getAllJavaBizVarCode(List<Element> allJavaEles) {
        InputStream fileStream = this.getClass().getClassLoader().getResourceAsStream("migrate/java三方因子对应数据源.xlsx");
        Optional<List<List<CellDataVO>>> readExcelData = Optional.of(ExcelUtils.readExcelData(fileStream, 2, Integer.MAX_VALUE, 1, Integer.MAX_VALUE));
        List<String> excelEleCodeList = readExcelData.get().stream().map(cell ->
                cell.get(0).getValue()).map(String::valueOf).collect(Collectors.toList());
        return allJavaEles.stream()
                .filter(ele -> !excelEleCodeList.contains(ele.getId()))
                .map(Element::getId)
                .collect(Collectors.toList());

    }

    //    @Transactional(rollbackFor = Exception.class)
    public void migrateJavaThirdPartyVars(List<Element> allJavaFuncEles, MigrateVarReqVO migrateVarReqVO) {
        log.info("start migrateJavaThirdPartyVars...");
        long startTime = System.currentTimeMillis();
        List<Variable> variableList;
        List<VariableInterface> variableInterfaces;
        List<VariableMigrateVO> variableMigrateVOList;
        InputStream fileStream = this.getClass().getClassLoader().getResourceAsStream("migrate/java三方因子对应数据源.xlsx");
        Optional<List<List<CellDataVO>>> readExcelData = Optional.of(ExcelUtils.readExcelData(fileStream, 2, Integer.MAX_VALUE, 1, Integer.MAX_VALUE));
        List<String> excelEleCodeList = readExcelData.get().stream().map(cell ->
                cell.get(0).getValue()).map(String::valueOf).collect(Collectors.toList());

        List<Element> alljavaThirdPartyEles = allJavaFuncEles.stream().filter(ele -> excelEleCodeList.contains(ele.getId())).collect(Collectors.toList());

        Map<String, VariableMigrateDTO> varCodeAndMigrateDTO = alljavaThirdPartyEles.stream().map(ele -> {
            VariableMigrateDTO variableMigrateDTO = new VariableMigrateDTO();
            variableMigrateDTO.setVarCode(ele.getId());
            variableMigrateDTO.setVarDefaultValue(ele.getDefaultValue());
            variableMigrateDTO.setVarName(ele.getName());
            variableMigrateDTO.setVarValueType(
                    Integer.valueOf(EnumUtil.fromNameOrThrow(VariableTypeEnum.class, ele.getType()).getCode())
            );
            return variableMigrateDTO;
        }).collect(Collectors.toMap(VariableMigrateDTO::getVarCode, Function.identity()));

        variableMigrateVOList = readExcelData.get().stream().map(cellData -> {
            String eleCode = (String) cellData.get(0).getValue();
            String interfaceId = ((String) cellData.get(1).getValue()).replaceAll("/", "_");
            String methodId = ((String) cellData.get(2).getValue()).replaceAll("/", "_");
            VariableMigrateDTO variableMigrateDTO = varCodeAndMigrateDTO.get(eleCode);
            return VariableMigrateVO.builder()
                    .creator("李尚勤")
                    .modifier("李尚勤")
                    .folderId(migrateVarReqVO.getJavaThirdPartyFolderId())
                    .dataSourceId(migrateVarReqVO.getJavaThirdPartyDataSourceId())
                    .datasourceType(0)
                    .interfaceId(migrateVarReqVO.getJavaThirdPartyDataSourceId())
                    .isCommon(0)
                    .isInner(0)
                    .isOutput(0)
                    .isInput(0)
                    .isDeleted("N")
                    .isUseType(2)
                    .provider(interfaceId)
                    .api(methodId)
                    .varCode(eleCode)
                    .varName(variableMigrateDTO.getVarName())
                    .varType(5)
                    .varValueType(variableMigrateDTO.getVarValueType())
                    .varDefaultValue(variableMigrateDTO.getVarDefaultValue())
                    .organId(1L)
                    .build();

        }).collect(Collectors.toList());

        variableList = Optional.ofNullable(variableMigrateVOList)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    Variable variable = new Variable();
                    variable.setCreator(var.getCreator());
                    variable.setModifier(var.getModifier());
                    variable.setVarName(var.getVarName());
                    variable.setVarCode(var.getVarCode());
                    variable.setVarDefaultValue(var.getVarDefaultValue());
                    variable.setIsUseType(var.getIsUseType());
                    variable.setIsOutput(var.getIsOutput());
                    variable.setIsInner(var.getIsInner());
                    variable.setIsInput(var.getIsInput());
                    variable.setIsCommon(var.getIsCommon());
                    variable.setDataSourceId(var.getDataSourceId());
                    variable.setVarType(var.getVarType());
                    variable.setStatus(1);
                    variable.setIsDeleted(var.getIsDeleted());
                    variable.setOrganId(var.getOrganId());
                    variable.setFolderId(var.getFolderId());
                    variable.setVarValueType(var.getVarValueType());
                    variable.setInterfaceId(var.getInterfaceId());

                    return variable;
                }).collect(Collectors.toList());


        variableInterfaces = Optional.ofNullable(variableMigrateVOList)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    VariableInterface variableInterface = new VariableInterface();
                    variableInterface.setApi(var.getApi());
                    variableInterface.setProvider(var.getProvider());
                    variableInterface.setDatasourceId(migrateVarReqVO.getJavaThirdPartyDataSourceId().intValue());
                    variableInterface.setVariableCode(var.getVarCode());
                    variableInterface.setDatasourceType(var.getDatasourceType());
                    return variableInterface;
                }).collect(Collectors.toList());

        List<Future<?>> futures = new ArrayList<>();
        List<List<Variable>> varPartitions = Lists.partition(variableList, 10);
        List<List<VariableInterface>> varInterfacePartitions = Lists.partition(variableInterfaces, 10);

        try {
            migrateVarAndInterface(futures, varPartitions, varInterfacePartitions);
        } catch (Exception e) {
            log.error("error in migrateJavaThirdPartyVars", e);
        }

        for (Future<?> future : futures) {
            try {
                future.get();
            } catch (Exception e) {
                log.error("error in migrateJavaThirdPartyVars", e);
            }
        }

        long endTime = System.currentTimeMillis();
        log.info("stop migrateJavaThirdPartyVars...timeCosuming = {}", endTime - startTime);
    }

    public void migrateJavaComboVars(List<Element> allJavaComboEles,
                                     MigrateVarReqVO migrateVarReqVO) {

        Set<String> comboCodeToFetch = allJavaComboEles.stream().map(Element::getId).collect(Collectors.toSet());
        log.info("start migrateJavaComboVars...comboCodeToFetch = {} size ={}", JSONObject.toJSONString(comboCodeToFetch), comboCodeToFetch.size());
        long startTime = System.currentTimeMillis();
        List<Variable> variableList;
        List<VariableInterface> variableInterfaces;
        List<VariableMigrateVO> variableMigrateVOList;

        Map<String, VariableMigrateDTO> varCodeAndMigrateDTO = allJavaComboEles.stream().map(ele -> {
            VariableMigrateDTO variableMigrateDTO = new VariableMigrateDTO();
            variableMigrateDTO.setVarCode(ele.getId());
            variableMigrateDTO.setVarDefaultValue(ele.getDefaultValue());
            variableMigrateDTO.setVarName(ele.getName());
            variableMigrateDTO.setVarValueType(
                    Integer.valueOf(EnumUtil.fromNameOrThrow(VariableTypeEnum.class, ele.getType()).getCode())
            );
            return variableMigrateDTO;
        }).collect(Collectors.toMap(VariableMigrateDTO::getVarCode, Function.identity()));

        variableMigrateVOList = varCodeAndMigrateDTO.values().stream().map(var -> {
            String eleCode = var.getVarCode();
            String interfaceId = ProviderEnum.JAVA_COMBO.getCode();
            String methodId = ApiEnum.JAVA_COMBO_VAR.getCode();
            Integer valueType = var.getVarValueType();
            String defaultValue = var.getVarDefaultValue();
            String varName = var.getVarName();
            return VariableMigrateVO.builder()
                    .creator("李尚勤")
                    .modifier("李尚勤")
                    .folderId(migrateVarReqVO.getJavaComboFolderId())
                    .dataSourceId(migrateVarReqVO.getJavaComboDatasourceId())
                    .datasourceType(0)
                    .interfaceId(migrateVarReqVO.getJavaComboDatasourceId())
                    .isCommon(0)
                    .isInner(0)
                    .isOutput(0)
                    .isInput(0)
                    .isDeleted("N")
                    .isUseType(2)
                    .provider(interfaceId)
                    .api(methodId)
                    .varCode(eleCode)
                    .varName(varName)
                    .varType(5)
                    .varValueType(valueType)
                    .varDefaultValue(defaultValue)
                    .organId(1L)
                    .build();

        }).collect(Collectors.toList());

        variableList = Optional.ofNullable(variableMigrateVOList)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    Variable variable = new Variable();
                    variable.setCreator(var.getCreator());
                    variable.setModifier(var.getModifier());
                    variable.setVarName(var.getVarName());
                    variable.setVarCode(var.getVarCode());
                    variable.setVarDefaultValue(var.getVarDefaultValue());
                    variable.setIsUseType(var.getIsUseType());
                    variable.setIsOutput(var.getIsOutput());
                    variable.setIsInner(var.getIsInner());
                    variable.setIsInput(var.getIsInput());
                    variable.setIsCommon(var.getIsCommon());
                    variable.setDataSourceId(var.getDataSourceId());
                    variable.setVarType(var.getVarType());
                    variable.setStatus(1);
                    variable.setIsDeleted(var.getIsDeleted());
                    variable.setOrganId(var.getOrganId());
                    variable.setFolderId(var.getFolderId());
                    variable.setVarValueType(var.getVarValueType());
                    variable.setInterfaceId(var.getInterfaceId());
                    return variable;
                }).collect(Collectors.toList());

        variableInterfaces = Optional.ofNullable(variableMigrateVOList)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    VariableInterface variableInterface = new VariableInterface();
                    variableInterface.setApi(var.getApi());
                    variableInterface.setProvider(var.getProvider());
                    variableInterface.setDatasourceId(migrateVarReqVO.getJavaComboDatasourceId().intValue());
                    variableInterface.setVariableCode(var.getVarCode());
                    variableInterface.setDatasourceType(var.getDatasourceType());
                    return variableInterface;
                }).collect(Collectors.toList());

        List<Future<?>> futures = new ArrayList<>();
        List<List<Variable>> varPartitions = Lists.partition(variableList, 10);
        List<List<VariableInterface>> varInterfacePartitions = Lists.partition(variableInterfaces, 10);
        log.info("variableList ={} to insert  variableInterfaces = {} to insert", JSONObject.toJSONString(variableList), JSONObject.toJSONString(variableInterfaces));
        try {
            migrateVarAndInterface(futures, varPartitions, varInterfacePartitions);
        } catch (Exception e) {
            log.error("error in migrateJavaComboVars", e);
        }
        for (Future<?> future : futures) {
            try {
                future.get();
            } catch (Exception e) {
                log.error("error in migrateJavaComboVars", e);
            }
        }

        long endTime = System.currentTimeMillis();
        log.info("stop migrateJavaComboVars...timeConsuming ={}", endTime - startTime);

    }


    //    @Transactional(rollbackFor = Exception.class)
    public void migrateJavaBizVars(List<Element> allJavaFuncEles,
                                   List<String> javaBizEleCodeList,
                                   MigrateVarReqVO migrateVarReqVO) {

        log.info("start migrateJavaBizVars...");
        long startTime = System.currentTimeMillis();
        List<Variable> variableList;
        List<VariableInterface> variableInterfaces;
        List<VariableMigrateVO> variableMigrateVOList;

        List<Element> javaBizEleList = allJavaFuncEles.stream().filter(ele -> javaBizEleCodeList.contains(ele.getId()))
                .collect(Collectors.toList());

        Map<String, VariableMigrateDTO> varCodeAndMigrateDTO = javaBizEleList.stream().map(ele -> {
            VariableMigrateDTO variableMigrateDTO = new VariableMigrateDTO();
            variableMigrateDTO.setVarCode(ele.getId());
            variableMigrateDTO.setVarDefaultValue(ele.getDefaultValue());
            variableMigrateDTO.setVarName(ele.getName());
            variableMigrateDTO.setVarValueType(
                    Integer.valueOf(EnumUtil.fromNameOrThrow(VariableTypeEnum.class, ele.getType()).getCode())
            );
            return variableMigrateDTO;
        }).collect(Collectors.toMap(VariableMigrateDTO::getVarCode, Function.identity()));

        variableMigrateVOList = varCodeAndMigrateDTO.values().stream().map(var -> {
            String eleCode = var.getVarCode();
            String interfaceId = ProviderEnum.JAVA_BIZ.getCode();
            String methodId = ApiEnum.JAVA_BIZ_VAR.getCode();
            Integer valueType = var.getVarValueType();
            String defaultValue = var.getVarDefaultValue();
            String varName = var.getVarName();
            return VariableMigrateVO.builder()
                    .creator("李尚勤")
                    .modifier("李尚勤")
                    .folderId(migrateVarReqVO.getJavaBizFolderId())
                    .dataSourceId(migrateVarReqVO.getJavaBizDatasourceId())
                    .datasourceType(0)
                    .interfaceId(migrateVarReqVO.getJavaBizDatasourceId())
                    .isCommon(0)
                    .isInner(0)
                    .isOutput(0)
                    .isInput(0)
                    .isDeleted("N")
                    .isUseType(2)
                    .provider(interfaceId)
                    .api(methodId)
                    .varCode(eleCode)
                    .varName(varName)
                    .varType(5)
                    .varValueType(valueType)
                    .varDefaultValue(defaultValue)
                    .organId(1L)
                    .build();

        }).collect(Collectors.toList());

        variableList = Optional.ofNullable(variableMigrateVOList)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    Variable variable = new Variable();
                    variable.setCreator(var.getCreator());
                    variable.setModifier(var.getModifier());
                    variable.setVarName(var.getVarName());
                    variable.setVarCode(var.getVarCode());
                    variable.setVarDefaultValue(var.getVarDefaultValue());
                    variable.setIsUseType(var.getIsUseType());
                    variable.setIsOutput(var.getIsOutput());
                    variable.setIsInner(var.getIsInner());
                    variable.setIsInput(var.getIsInput());
                    variable.setIsCommon(var.getIsCommon());
                    variable.setDataSourceId(var.getDataSourceId());
                    variable.setVarType(var.getVarType());
                    variable.setStatus(1);
                    variable.setIsDeleted(var.getIsDeleted());
                    variable.setOrganId(var.getOrganId());
                    variable.setFolderId(var.getFolderId());
                    variable.setVarValueType(var.getVarValueType());
                    variable.setInterfaceId(var.getInterfaceId());
                    return variable;
                }).collect(Collectors.toList());

        variableInterfaces = Optional.ofNullable(variableMigrateVOList)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    VariableInterface variableInterface = new VariableInterface();
                    variableInterface.setApi(var.getApi());
                    variableInterface.setProvider(var.getProvider());
                    variableInterface.setDatasourceId(migrateVarReqVO.getJavaBizDatasourceId().intValue());
                    variableInterface.setVariableCode(var.getVarCode());
                    variableInterface.setDatasourceType(var.getDatasourceType());
                    return variableInterface;
                }).collect(Collectors.toList());

        List<Future<?>> futures = new ArrayList<>();
        List<List<Variable>> varPartitions = Lists.partition(variableList, 10);
        List<List<VariableInterface>> varInterfacePartitions = Lists.partition(variableInterfaces, 10);
        try {
            migrateVarAndInterface(futures, varPartitions, varInterfacePartitions);
        } catch (Exception e) {
            log.error("error in migrateJavaBasicVars", e);
        }
        for (Future<?> future : futures) {
            try {
                future.get();
            } catch (Exception e) {
                log.error("error in migrateJavaBizVars", e);
            }
        }

        long endTime = System.currentTimeMillis();
        log.info("stop migrateJavaBizVars...timeConsuming ={}", endTime - startTime);

    }

    //    @Transactional(rollbackFor = Exception.class)
    public void migratePythonBizVars(List<Element> pythonElements,
                                     List<List<String>> pythonVarConfigInfoList,
                                     MigrateVarReqVO migrateVarReqVO) {
        log.info("start migratePythonBizVars...");
        long startTime = System.currentTimeMillis();
        List<Variable> variableList;
        List<VariableInterface> variableInterfaces;
        List<VariableMigrateVO> variableMigrateVOList = new ArrayList<>();

        List<String> pythonEleCodeList = pythonElements.stream().map(Element::getId).collect(Collectors.toList());
        Map<String, VariableMigrateDTO> varCodeAndMigrateDTO = pythonElements.stream().map(ele -> {
            VariableMigrateDTO variableMigrateDTO = new VariableMigrateDTO();
            variableMigrateDTO.setVarCode(ele.getId());
            variableMigrateDTO.setVarDefaultValue(ele.getDefaultValue());
            variableMigrateDTO.setVarName(ele.getName());
            variableMigrateDTO.setVarValueType(
                    Integer.valueOf(EnumUtil.fromNameOrThrow(VariableTypeEnum.class, ele.getType()).getCode())
            );
            return variableMigrateDTO;
        }).collect(Collectors.toMap(VariableMigrateDTO::getVarCode, Function.identity()));

        String provider = ProviderEnum.PYTHON_BIZ.getCode();
        String api = ApiEnum.PYTHON_BIZ_VAR.getCode();
        if (CollectionUtils.isNotEmpty(pythonVarConfigInfoList)) {
            variableMigrateVOList = pythonVarConfigInfoList.parallelStream()
                    .filter(item -> pythonEleCodeList.contains(item.get(0)))
                    .map(ele -> {
                        String varCode = ele.get(0);
                        VariableMigrateDTO variableMigrateDTO = varCodeAndMigrateDTO.get(varCode);
                        return VariableMigrateVO.builder()
                                .creator("李尚勤")
                                .modifier("李尚勤")
                                .folderId(migrateVarReqVO.getPythonBizFolderId())
                                .dataSourceId(migrateVarReqVO.getPythonBizDataSourceId())
                                .datasourceType(0)
                                .interfaceId(migrateVarReqVO.getPythonBizDataSourceId())
                                .isCommon(0)
                                .isInner(0)
                                .isOutput(0)
                                .isInput(0)
                                .isDeleted("N")
                                .isUseType(2)
                                .provider(provider)
                                .api(api)
                                .varCode(varCode)
                                .varName(variableMigrateDTO.getVarName())
                                .varType(5)
                                .varValueType(variableMigrateDTO.getVarValueType())
                                .varDefaultValue(variableMigrateDTO.getVarDefaultValue())
                                .organId(1L)
                                .build();
                    })
                    .collect(Collectors.toList());
        }

        variableList = Optional.ofNullable(variableMigrateVOList)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    Variable variable = new Variable();
                    variable.setCreator(var.getCreator());
                    variable.setModifier(var.getModifier());
                    variable.setVarName(var.getVarName());
                    variable.setVarCode(var.getVarCode());
                    variable.setVarDefaultValue(var.getVarDefaultValue());
                    variable.setIsUseType(var.getIsUseType());
                    variable.setIsOutput(var.getIsOutput());
                    variable.setIsInner(var.getIsInner());
                    variable.setIsInput(var.getIsInput());
                    variable.setIsCommon(var.getIsCommon());
                    variable.setDataSourceId(var.getDataSourceId());
                    variable.setVarType(var.getVarType());
                    variable.setStatus(1);
                    variable.setIsDeleted(var.getIsDeleted());
                    variable.setOrganId(var.getOrganId());
                    variable.setFolderId(var.getFolderId());
                    variable.setVarValueType(var.getVarValueType());
                    variable.setInterfaceId(var.getInterfaceId());
                    return variable;
                }).collect(Collectors.toList());

        variableInterfaces = Optional.ofNullable(variableMigrateVOList)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    VariableInterface variableInterface = new VariableInterface();
                    variableInterface.setApi(var.getApi());
                    variableInterface.setProvider(var.getProvider());
                    variableInterface.setDatasourceId(migrateVarReqVO.getPythonBizDataSourceId().intValue());
                    variableInterface.setVariableCode(var.getVarCode());
                    variableInterface.setDatasourceType(var.getDatasourceType());
                    return variableInterface;
                }).collect(Collectors.toList());

        List<Future<?>> futures = new ArrayList<>();
        List<List<Variable>> varPartitions = Lists.partition(variableList, 10);
        List<List<VariableInterface>> varInterfacePartitions = Lists.partition(variableInterfaces, 10);
        try {
            migrateVarAndInterface(futures, varPartitions, varInterfacePartitions);
        } catch (Exception e) {
            log.error("error in migratePythonThirdPartyVars", e);
            throw new RuntimeException("error in migratePythonThirdPartyVars");
        }

        for (Future<?> future : futures) {
            try {
                future.get();
            } catch (Exception e) {
                log.error("error in migratePythonThirdPartyVars", e);
            }
        }

        long endTime = System.currentTimeMillis();
        log.info("stop migratePythonThirdPartyVars...timeConsuming ={}", endTime - startTime);
    }


    public void migratePythonModelVars(List<Element> pythonElements,
                                       MigrateVarReqVO migrateVarReqVO) {
        log.info("start migratePythonModelVars...");
        long startTime = System.currentTimeMillis();
        List<Variable> variableList;
        List<VariableInterface> variableInterfaces;
        List<VariableMigrateVO> variableMigrateVOList = new ArrayList<>();

        List<String> pythonEleCodeList = pythonElements.stream().map(Element::getId).collect(Collectors.toList());
        Map<String, VariableMigrateDTO> varCodeAndMigrateDTO = pythonElements.stream().map(ele -> {
            VariableMigrateDTO variableMigrateDTO = new VariableMigrateDTO();
            variableMigrateDTO.setVarCode(ele.getId());
            variableMigrateDTO.setVarDefaultValue(ele.getDefaultValue());
            variableMigrateDTO.setVarName(ele.getName());
            variableMigrateDTO.setVarValueType(
                    Integer.valueOf(EnumUtil.fromNameOrThrow(VariableTypeEnum.class, ele.getType()).getCode())
            );
            return variableMigrateDTO;
        }).collect(Collectors.toMap(VariableMigrateDTO::getVarCode, Function.identity()));

        String provider = "E".equals(migrateVarReqVO.getType()) ? ProviderEnum.PYTHON_MODEL_CACHE.getCode() : ProviderEnum.PYTHON_MODEL.getCode();
        String api = "E".equals(migrateVarReqVO.getType()) ? ApiEnum.PYTHON_MODEL_CACHE_VAR.getCode() : ApiEnum.PYTHON_MODEL_VAR.getCode();

        Long folderId = "E".equals(migrateVarReqVO.getType()) ? migrateVarReqVO.getPythonModelCacheFolderId() : migrateVarReqVO.getPythonModelFolderId();
        Long dataSourceId = "E".equals(migrateVarReqVO.getType()) ? migrateVarReqVO.getPythonModelCacheDataSourceId() : migrateVarReqVO.getPythonModelDataSourceId();

        if (MapUtils.isNotEmpty(varCodeAndMigrateDTO)) {
            variableMigrateVOList = varCodeAndMigrateDTO.values().parallelStream()
                    .filter(item -> pythonEleCodeList.contains(item.getVarCode()))
                    .map(ele -> {
                        String varCode = ele.getVarCode();
                        VariableMigrateDTO variableMigrateDTO = varCodeAndMigrateDTO.get(varCode);
                        return VariableMigrateVO.builder()
                                .creator("李尚勤")
                                .modifier("李尚勤")
                                .folderId(folderId)
                                .dataSourceId(dataSourceId)
                                .datasourceType(0)
                                .interfaceId(dataSourceId)
                                .isCommon(0)
                                .isInner(0)
                                .isOutput(0)
                                .isInput(0)
                                .isDeleted("N")
                                .isUseType(2)
                                .provider(provider)
                                .api(api)
                                .varCode(varCode)
                                .varName(variableMigrateDTO.getVarName())
                                .varType(5)
                                .varValueType(variableMigrateDTO.getVarValueType())
                                .varDefaultValue(variableMigrateDTO.getVarDefaultValue())
                                .organId(1L)
                                .build();
                    })
                    .collect(Collectors.toList());
        }

        variableList = Optional.ofNullable(variableMigrateVOList)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    Variable variable = new Variable();
                    variable.setCreator(var.getCreator());
                    variable.setModifier(var.getModifier());
                    variable.setVarName(var.getVarName());
                    variable.setVarCode(var.getVarCode());
                    variable.setVarDefaultValue(var.getVarDefaultValue());
                    variable.setIsUseType(var.getIsUseType());
                    variable.setIsOutput(var.getIsOutput());
                    variable.setIsInner(var.getIsInner());
                    variable.setIsInput(var.getIsInput());
                    variable.setIsCommon(var.getIsCommon());
                    variable.setDataSourceId(var.getDataSourceId());
                    variable.setVarType(var.getVarType());
                    variable.setStatus(1);
                    variable.setIsDeleted(var.getIsDeleted());
                    variable.setOrganId(var.getOrganId());
                    variable.setFolderId(var.getFolderId());
                    variable.setVarValueType(var.getVarValueType());
                    variable.setInterfaceId(var.getInterfaceId());
                    return variable;
                }).collect(Collectors.toList());

        variableInterfaces = Optional.of(variableMigrateVOList)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    VariableInterface variableInterface = new VariableInterface();
                    variableInterface.setApi(var.getApi());
                    variableInterface.setProvider(var.getProvider());
                    variableInterface.setDatasourceId(dataSourceId.intValue());
                    variableInterface.setVariableCode(var.getVarCode());
                    variableInterface.setDatasourceType(var.getDatasourceType());
                    return variableInterface;
                }).collect(Collectors.toList());

        List<Future<?>> futures = new ArrayList<>();
        List<List<Variable>> varPartitions = Lists.partition(variableList, 10);
        List<List<VariableInterface>> varInterfacePartitions = Lists.partition(variableInterfaces, 10);
        try {
            migrateVarAndInterface(futures, varPartitions, varInterfacePartitions);
        } catch (Exception e) {
            log.error("error in migratePythonModelVars", e);
            throw new RuntimeException("error in migratePythonModelVars");
        }

        for (Future<?> future : futures) {
            try {
                future.get();
            } catch (Exception e) {
                log.error("error in migratePythonModelVars", e);
            }
        }

        long endTime = System.currentTimeMillis();
        log.info("stop migratePythonModelVars...timeConsuming ={}", endTime - startTime);
    }


    //    @Transactional(rollbackFor = Exception.class)
    public void migratePythonThirdPartyVars(List<Element> pythonElements,
                                            List<List<String>> pythonVarConfigInfoList,
                                            MigrateVarReqVO migrateVarReqVO) {
        log.info("start migratePythonBizVars...");
        long startTime = System.currentTimeMillis();
        List<Variable> variableList;
        List<VariableInterface> variableInterfaces;
        List<VariableMigrateVO> variableMigrateVOList = new ArrayList<>();

        List<String> pythonEleCodeList = pythonElements.stream().map(Element::getId).collect(Collectors.toList());
        Map<String, VariableMigrateDTO> varCodeAndMigrateDTO = pythonElements.stream().map(ele -> {
            VariableMigrateDTO variableMigrateDTO = new VariableMigrateDTO();
            variableMigrateDTO.setVarCode(ele.getId());
            variableMigrateDTO.setVarDefaultValue(ele.getDefaultValue());
            variableMigrateDTO.setVarName(ele.getName());
            variableMigrateDTO.setVarValueType(
                    Integer.valueOf(EnumUtil.fromNameOrThrow(VariableTypeEnum.class, ele.getType()).getCode())
            );
            return variableMigrateDTO;
        }).collect(Collectors.toMap(VariableMigrateDTO::getVarCode, Function.identity()));
        Long dataSrouceId = "M".equals(migrateVarReqVO.getType()) ? migrateVarReqVO.getPythonModelDataSourceId() :
                "C".equals(migrateVarReqVO.getType()) ?
                        migrateVarReqVO.getPythonThirdPartyCacheDataSourceId() : migrateVarReqVO.getPythonThirdPartyDataSourceId();
        if (CollectionUtils.isNotEmpty(pythonVarConfigInfoList)) {
            variableMigrateVOList = pythonVarConfigInfoList.parallelStream()
                    .filter(item -> pythonEleCodeList.contains(item.get(0)))
                    .map(ele -> {
                        String varCode = ele.get(0);
                        String varValueType = ele.get(1);
                        JSONArray providerArray = JSON.parseObject(JSON.toJSONString(ele.get(2)), JSONArray.class);
                        String provider = providerArray.size() <= 1 ? (String) providerArray.get(0) :
                                StringUtils.join(providerArray, "_");
                        JSONArray apiArray = JSON.parseObject(JSON.toJSONString(ele.get(3)), JSONArray.class);
                        String api = apiArray.size() <= 1 ? (String) apiArray.get(0) :
                                StringUtils.join(apiArray, "_");
                        Long folderId = "M".equals(migrateVarReqVO.getType()) ? migrateVarReqVO.getPytonThirdPartyFolderId() :
                                "C".equals(migrateVarReqVO.getType()) ?
                                        migrateVarReqVO.getPythonThridPartyCacheFolderId() : migrateVarReqVO.getPythonModelFolderId();


                        VariableMigrateDTO variableMigrateDTO = varCodeAndMigrateDTO.get(varCode);
                        return VariableMigrateVO.builder()
                                .creator("李尚勤")
                                .modifier("李尚勤")
                                .folderId(folderId)
                                .dataSourceId(dataSrouceId)
                                .datasourceType(0)
                                .interfaceId(dataSrouceId)
                                .isCommon(0)
                                .isInner(0)
                                .isOutput(0)
                                .isInput(0)
                                .isDeleted("N")
                                .isUseType(2)
                                .provider(provider)
                                .api(api)
                                .varCode(varCode)
                                .varName(variableMigrateDTO.getVarName())
                                .varType(5)
                                .varValueType(variableMigrateDTO.getVarValueType())
                                .varDefaultValue(variableMigrateDTO.getVarDefaultValue())
                                .organId(1L)
                                .build();
                    })
                    .collect(Collectors.toList());
        }

        variableList = Optional.ofNullable(variableMigrateVOList)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    Variable variable = new Variable();
                    variable.setCreator(var.getCreator());
                    variable.setModifier(var.getModifier());
                    variable.setVarName(var.getVarName());
                    variable.setVarCode(var.getVarCode());
                    variable.setVarDefaultValue(var.getVarDefaultValue());
                    variable.setIsUseType(var.getIsUseType());
                    variable.setIsOutput(var.getIsOutput());
                    variable.setIsInner(var.getIsInner());
                    variable.setIsInput(var.getIsInput());
                    variable.setIsCommon(var.getIsCommon());
                    variable.setDataSourceId(var.getDataSourceId());
                    variable.setVarType(var.getVarType());
                    variable.setStatus(1);
                    variable.setIsDeleted(var.getIsDeleted());
                    variable.setOrganId(var.getOrganId());
                    variable.setFolderId(var.getFolderId());
                    variable.setVarValueType(var.getVarValueType());
                    variable.setInterfaceId(var.getInterfaceId());
                    return variable;
                }).collect(Collectors.toList());

        variableInterfaces = Optional.ofNullable(variableMigrateVOList)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(var -> {
                    VariableInterface variableInterface = new VariableInterface();
                    variableInterface.setApi(var.getApi());
                    variableInterface.setProvider(var.getProvider());
                    variableInterface.setDatasourceId(dataSrouceId.intValue());
                    variableInterface.setVariableCode(var.getVarCode());
                    variableInterface.setDatasourceType(var.getDatasourceType());
                    return variableInterface;
                }).collect(Collectors.toList());

        List<Future<?>> futures = new ArrayList<>();
        List<List<Variable>> varPartitions = Lists.partition(variableList, 10);
        List<List<VariableInterface>> varInterfacePartitions = Lists.partition(variableInterfaces, 10);

        try {
            migrateVarAndInterface(futures, varPartitions, varInterfacePartitions);
        } catch (Exception e) {
            log.error("error in migratePythonThirdPartyVars", e);
        }

        for (Future<?> future : futures) {
            try {
                future.get();
            } catch (Exception e) {
                log.error("error in migratePythonThirdPartyVars", e);
            }
        }

        long endTime = System.currentTimeMillis();
        log.info("stop migratePythonThirdPartyVars...timeConsuming ={}", endTime - startTime);
    }


    private void migrateVarAndInterface(List<Future<?>> futures,
                                        List<List<Variable>> varPartitions,
                                        List<List<VariableInterface>> varInterfacePartitions) {
        varPartitions.forEach(var -> {
            futures.add(migrateExecutor.submit(() -> {
                variableService.batchInsertOrUpdateVariables(var);
            }));
        });
        varInterfacePartitions.forEach(varInterface -> {
            futures.add(migrateExecutor.submit(() -> {
                varInterfaceRelService.batchInsertOrUpdateVariables(varInterface);
            }));
        });
    }


    public static void main(String[] args) {
        MigrateVarService migrateVarService = new MigrateVarService();
        List<List<String>> pythonThirdPartyVars = migrateVarService.getPythonThirdPartyVars();

        JSONArray objects = JSON.parseObject(pythonThirdPartyVars.get(0).get(3), JSONArray.class);
        String provider = (String) objects.get(0);
        System.out.println("pythonThirdPartyVars:" + JSONObject.toJSONString(pythonThirdPartyVars) + "provider: " + provider);

    }


}
